package com.dummies.android.taskreminder;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.TextView;

public class ReminderListActivity extends android.app.ListActivity {
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_EDIT = 1;
	
	private RemindersDbAdapter mDbHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reminder_list);
		
		mDbHelper = new RemindersDbAdapter(this);
		mDbHelper.open();
		fillData();

		registerForContextMenu(getListView());
	}


	public static final String DATE_TIME_FORMAT = "yyyy-MM-dd kk:mm:ss";
	@SuppressLint("SimpleDateFormat")
	@SuppressWarnings("unused")
	private void defaultTask() {
		Calendar mCalendar = Calendar.getInstance();
		SimpleDateFormat dateTimeFormat = new
				 SimpleDateFormat(DATE_TIME_FORMAT);
		String reminderDateTime = dateTimeFormat.format(mCalendar.getTime()); 
		long id = mDbHelper.createReminder("task1", "body1", reminderDateTime);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Intent i = new Intent(this, ReminderEditActivity.class);
		
		i.putExtra(RemindersDbAdapter.KEY_ROWID, id);
		startActivityForResult(i, ACTIVITY_EDIT);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater mi = getMenuInflater();
		mi.inflate(R.menu.list_menu_item_longpress, menu);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		MenuInflater mi = getMenuInflater();
		mi.inflate(R.menu.list_menu, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.menu_insert:
			createReminder();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.menu_delete:
			Log.d("==menu_delete==", "onContextItemSelected");
			// Delete the task
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
					.getMenuInfo();
			mDbHelper.deleteReminder(info.id);
			fillData();
			break;
		case R.id.menu_edit:
			Log.d("==menu_edit==", "onContextItemSelected");
			AdapterContextMenuInfo infoEdit = (AdapterContextMenuInfo) item
					.getMenuInfo();
			Intent intent = new Intent(this, ReminderEditActivity.class);
			intent.putExtra(RemindersDbAdapter.KEY_ROWID, infoEdit.id);
			startActivity(intent);
			break;
		}
		return super.onContextItemSelected(item);
	}

	private void createReminder() {
		Intent i = new Intent(this, ReminderEditActivity.class);
		startActivityForResult(i, ACTIVITY_CREATE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		fillData();
	}

	@SuppressWarnings("deprecation")
	private void fillData() {
		Cursor reminderCursor = mDbHelper.fetchAllReminders();
		startManagingCursor(reminderCursor);
		Log.d("==fillData==", ""+reminderCursor.getCount());
		reminderCursor.moveToFirst();

		String[] from = new String[] { RemindersDbAdapter.KEY_TITLE , RemindersDbAdapter.KEY_DATE_TIME};
		int[] to = new int[] { R.id.text1, R.id.duration };
		SimpleCursorAdapter reminders = new SimpleCursorAdapter(this,
				R.layout.reminder_row, reminderCursor, from, to);
		Log.d("==fillData==", "adapter.count=" + reminders.getCount());
		setListAdapter(reminders);
	}
}
